/**
 * @Date: Mar 8, 2010 8:07:06 PM
 */
package com.philip.journal.home.controller.action;

import java.io.File;
import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;

import com.philip.journal.core.controller.ResponseHandler;
import com.philip.journal.core.exception.JournalException;
import com.philip.journal.core.service.ServiceProxy;

/**
 * @author cry30
 */
public class ImportCodeXmlAction extends AbstractTreeAction {

    /** Temporary path to uploaded file. IOC container configured. */
    private transient String temporaryPath;

    /** Max memory size required by commons DiskFileItemFactory. IOC container configured. */
    private transient int maxMemorySize;

    /** Max size required by commons ServletFileUpload. IOC container configured. */
    private transient int maxRequestSize;

    /**
     * Default delegate constructor.
     *
     * @param serviceProxy Service proxy to be injected by Spring.
     * @param responseHandler Response handler to be injected by Spring.
     */
    public ImportCodeXmlAction(final ServiceProxy serviceProxy, final ResponseHandler responseHandler) {
        super(serviceProxy, responseHandler);
    }

    @SuppressWarnings("unchecked")
    @Override
    public void execute() throws ServletException, IOException {
        try {
            final DiskFileItemFactory factory = new DiskFileItemFactory(maxMemorySize, new File(temporaryPath));
            final ServletFileUpload upload = new ServletFileUpload(factory);
            upload.setSizeMax(maxRequestSize);
            final List<FileItem> items = upload.parseRequest(getHttpServletRequest());
            for (final FileItem nextItem : items) {
                if (!nextItem.isFormField()) {
                    getServiceProxy().importCodeXML(nextItem.getInputStream());
                }
            }
        } catch (final JournalException e) {
            getLogger().debug(e.getMessage(), e);
            getResponseHandler().respondFail(getHttpServletResponse(), e.getMessage());
        } catch (final FileUploadException e) {
            getLogger().debug(e.getMessage(), e);
            getResponseHandler().respondFail(getHttpServletResponse(), e.getMessage());
        }
    }

    /**
     * IOC setter method for the temporary path.
     * 
     * @param pTempPath new temporary path.
     */
    public void setTemporaryPath(final String pTempPath) {
        this.temporaryPath = pTempPath;
    }

    public void setMaxMemorySize(final int pMaxMemorySize) {
        this.maxMemorySize = pMaxMemorySize;
    }


    public void setMaxRequestSize(final int pMaxRequestSize) {
        this.maxRequestSize = pMaxRequestSize;
    }

}
